<?php

require_once( '../system/smarty.inc' );
require_once( '../system/mdao/Class_M_PATTERN.php' );
require_once( '../system/mdao/Class_M_PATTERN_PRICE.php' );
require_once( '../system/mdao/Class_M_PATTERN_BLOCK.php' );
require_once( '../system/Class_ERROR.php' );
require_once( '../system/Class_PWD.php' );
require_once( '../system/login.inc.php' );

Class_PWD::valid_auth(AOKI_AUTH_PATTERN,$login['staff_auth']);

$dao    = new Class_M_PATTERN;
$daoB   = new Class_M_PATTERN_BLOCK($dao->get_db());
$daoP   = new Class_M_PATTERN_PRICE($dao->get_db());
//$block = Class_M_OTHER::get_block_array(PLEASE_SELECT_ARR);

if( $_SERVER['REQUEST_METHOD'] == 'GET' ){
	$phase = 'input';
	if( isset($_GET['id']) && $_GET['id'] != '' ){
		$forms  = $dao->get( $_GET['id'] );
		$forms["id"] = $_GET['id'];
		//$forms["stop_date"] = Class_PWD::int8_to_date($forms["stop_date"]) ;
		//で
		$data  = $daoP->getbypattern($forms['pattern_cd'] ) ;
		foreach  ($data as $k=>$v) {
			if ($v['size'] == "B2") {
				$forms['priceb2']  =$v['price'];
			}
			if ($v['size'] == "B3") {
				$forms['priceb3']  =$v['price'];
			}
			if ($v['size'] == "B4") {
				$forms['priceb4']  =$v['price'];
			}
		}	 
	}else{
		//$forms['auth'] = 0;
	}
	$dbarray = $daoB->getFormBlock($forms['id'] );

	//データ整形
	foreach ($dbarray as $k=>$v) {
		$pchk = empty($v["pblock_cd"]) ? 0:1;
		$block[] = array( "block_cd"=> $v["block_cd"], "block_name" => $v["block_name"], "pchk" => $pchk );
	}
}else{
	$forms = $_POST;
	if( $forms['mode'] == 'input' ){
		$err     = new Class_ERROR;
		$chk     = $dao->get_checks();
		foreach ($forms["block_cd"] as $k=>$v) {
				$elecchk = in_array($v, $forms["bid"]) ? 1:0;
				$block[] = array( "block_cd"=> $v ,"block_name" => $forms["block_name"][$k], "pchk" => $elecchk );
		}
		
		foreach ($chk as $kcol => $chkval) {
			$err_mes[$kcol]  = $err->check( $forms[$kcol], $chkval );
		}
		/*
		if (!empty( $forms["block_cd"])) {
			if (empty($block[$forms["block_cd"]])) {
				$err_mes["block_cd"]  = $err->format_msg('該当ブロックはマスタに存在しません');
			}
		}
		*/
		if  ( ( $forms["media_flg"] == 2 ) || ( $forms["media_flg"] == 3 ) )  {
				$err_mes["priceb2"] = $err->check( $forms["priceb2"], array("NUM") );
				$err_mes["priceb3"] = $err->check( $forms["priceb3"], array("NUM") );
				$err_mes["priceb4"] = $err->check( $forms["priceb4"], array("NUM") );
		}else{
			if (empty( $forms["bid"])) {
				$err_mes["bid"]  =  $err->format_msg('ブロックを選択してください');
			}
		}
		
		if ( $err->clear ){
			$cols     = $dao->get_cols();
			foreach($cols as $kcol => $val) {
				$values[$kcol] = $forms[$kcol] ;
			}
			$values["disp_ng"] = $forms["disp_ng"]?$forms["disp_ng"]:0;
			
			
			//桁数チェック
			$maxlens = $dao->get_maxlens();
			foreach ( $maxlens as $k => $v ) {
				$err_mes[$k] =$err->check_size($values[$k], $v);
			}
		}
		if ( $err->clear ){
			if( $forms['id'] == '' ){
				$where = sprintf(" pattern_name  = '%s'", $forms['pattern_name'] );
			}else{
				$where = sprintf(" pattern_name = '%s' AND pattern_cd  <> %s", $forms['pattern_name'], $forms['id'] );
			}
			if ( $dao->exits($where) ) {
				$err->clear = false;
				$err_mes['pattern_name'] = '<p class="error">このパターン名は重複しています</p>';
			}
		}
		//print_r($err_mes);
		if( $err->clear ){
			try {	
				$dao->begin_trans();
				$executeflg = TRUE;
				
				if( $forms['id'] == '' ){
					if ( $id = $dao->add($values)) {
						$forms['id'] = $id;
					} else {
						$executeflg = false;
					}
				}else{
					
					if ($dao->edit( $forms['id'], $values )) {
					} else {
						$executeflg = false;
					}
					$id = $forms['id'];
				}
				if ($executeflg ) {
					//追加のデータ作成
					$daoB->del(" pattern_cd = ".$forms['id'] );
					foreach($forms["bid"] as $k=>$v) {
						$values = array();
						$values["pattern_cd"] 	= $forms['id'];
						$values["block_cd"]   	= $v ;
						
						if ($daoB->addnotime($values)) {
						} else {
							$executeflg = FALSE;
						}
						
					}
				}
				if ($executeflg ) {
					//単価
					
					if  ( ( $forms["media_flg"] == 2 ) || ( $forms["media_flg"] == 3 ) )  {
						$daoP->del(" pattern_cd = ".$forms['id'] );
						$values = array();
						$values["pattern_cd"] 	= $forms['id'];
						$values["size"]     	= 'B2' ;
						$values["price"]     	= empty($forms["priceb2"]) ? 0 : $forms["priceb2"]  ;
						if ($daoP->addnotime($values)) {
						} else {
							$executeflg = FALSE;
						}
						$values = array();
						$values["pattern_cd"] 	= $forms['id'];
						$values["size"]     	= 'B3' ;
						$values["price"]     	= empty($forms["priceb3"]) ? 0 : $forms["priceb3"]  ;
						if ($daoP->addnotime($values)) {
						} else {
							$executeflg = FALSE;
						}
						$values = array();
						$values["pattern_cd"] 	= $forms['id'];
						$values["size"]     	= 'B4' ;
						$values["price"]     	= empty($forms["priceb2"]) ? 0 : $forms["priceb4"]  ;
						if ($daoP->addnotime($values)) {
						} else {
							$executeflg = FALSE;
						}

					}	
				}
				if  ($executeflg) {
					$dao->commit_trans();
					$smarty->assign('message', '更新が完了しました。');
				} else {
					$dao->rollback_trans();
					$smarty->assign('message', '更新が失敗しました。');
				}
			} catch (Exception $e) {
				$dao->rollback_trans();
				$smarty->assign('message', 'ＤＢエラーで失敗しました。');
			}
			
			$phase = 'complete';
		}else{
			$phase = 'input';
		}
	}
}

if( isset( $forms ) )   $smarty->assign('forms', $forms);
if( isset( $err_mes ) ) $smarty->assign('err', $err_mes);

if ($phase != 'complete') {
	
}

$smarty->assign('block', $block );

$dspflags[""] = "すべて";
foreach ($aoki_disp_flags as $k => $v) {
	$dspflags[$k] = $v;
}

$smarty->assign('dspflags', $dspflags );

$smarty->assign('flags', $aoki_pattern_flags );

$smarty->assign('phase', $phase );
$smarty->assign('menu', $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );

$smarty->display('mpattern_form.html');
?>